import { query } from './query';

/**
 * 获取元素的所有兄弟节点
 * @param element 目标元素
 * @returns 兄弟节点数组
 */
export function getSiblings(element: HTMLElement | null): HTMLElement[] {
    const siblings: HTMLElement[] = [];
    let sibling = element?.parentNode?.firstChild;

    while (sibling) {
        if (sibling.nodeType === 1 && sibling !== element) {
            siblings.push(sibling as HTMLElement);
        }

        sibling = sibling.nextSibling;
    }

    return siblings;
}

/**
 * 计算HTML字符串渲染后的宽度
 * @param htmlString HTML字符串
 * @returns 宽度值
 */
export function calculateElementWidth(htmlString: string) {
    const tempContainer = document.createElement("div");
    tempContainer.style.position = "absolute";
    tempContainer.style.top = "-10000px";
    tempContainer.innerHTML = htmlString;

    const element = tempContainer.children?.[0] as HTMLElement;
    element.style.boxSizing = "border-box";
    element.style.fontSize = "14px";

    document.body.appendChild(tempContainer);

    const width = element.offsetWidth;

    document.body.removeChild(tempContainer);

    return width;
}